﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class ThemPhieuDeNghiMuaHangForm : Form
    {
        private string _maPhieu = null;
        private bool _edit = false;
        private bool _success = false;

        public ThemPhieuDeNghiMuaHangForm()
        {
            InitializeComponent();
        }

        public ThemPhieuDeNghiMuaHangForm(string MaPhieu)
        {
            InitializeComponent();
            _edit = true;
            _maPhieu = MaPhieu;
        }

        private void ThemPhieuMuaHangForm_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            // Populate combobox
            ItemName.DisplayMember = "TenHangHoa";
            ItemName.ValueMember = "Self";
            var items = from i in dc.HangHoas where i.deleted == false select i;
            ItemName.DataSource = items;

            // show info
            if (_edit)
            {
                // info
                _DB.PhieuDeNghiMuaHang p = dc.PhieuDeNghiMuaHangs.Single(phieu => phieu.MaPhieu == _maPhieu);
                dtpNgayHan.Value = (DateTime)p.NgayHan;
                dtpNgayTao.Value = (DateTime)p.NgayTao;
                txtContent.Text = p.NoiDung;
                txtMaPhieu.Text = p.MaPhieu;
                txtMaPhieu.ReadOnly = true;

                // grid
                int count = 1;
                foreach (_DB.PhieuDeNghiMuaHang_HangHoa h in p.PhieuDeNghiMuaHang_HangHoas)
                {
                    DataGridViewRow r = new DataGridViewRow();
                    r.CreateCells(dgvHangHoa);
                    r.Cells[0].Value = count++;
                    r.Cells[1].Value = items.Single(i => i.MaHangHoa == h.MaHangHoa);
                    r.Cells[2].Value = h.SoLuongDeNghi.ToString();
                    r.Cells[3].Value = h.HangHoa.DonVi;
                    r.Cells[4].Value = h.NhaCungCapThamKhao;
                    r.Cells[5].Value = h.GiaThamKhao.ToString();

                    dgvHangHoa.Rows.Add(r);
                }

                // xin duyet?
                if (p.QuyTrinhMuaHangs.Single().TrangThai != _DB.TrangThaiQuyTrinhMuaHang.CHINH_SUA_PHIEU_DE_NGHI &&
                    p.QuyTrinhMuaHangs.Single().TrangThai != _DB.TrangThaiQuyTrinhMuaHang.KHONG_DUYET_PHIEU_DE_NGHI)
                {
                    xinDuyetBtn.Enabled = false;
                }
            }
            else
                txtMaPhieu.Text = _DB.DinhDangMaPhieu.getMaPhieuMoi(_DB.DinhDangMaPhieu.DINH_DANG_PHIEU_DE_NGHI_ID);

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                // connection db
                _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
                _DB.PhieuDeNghiMuaHang phieu;

                if (!_edit)
                {
                    phieu = new _DB.PhieuDeNghiMuaHang();
                    phieu.MaPhieu = txtMaPhieu.Text;
                    phieu.MaNguoiTao = Program.MaNguoiDungHienTai;

                    _DB.QuyTrinhMuaHang qt = new _DB.QuyTrinhMuaHang();
                    qt.MaPhieuDeNghiMuaHang = phieu.MaPhieu;
                    qt.TenQuyTrinh = DateTime.Now.ToString();

                    if (sender == xinDuyetBtn)
                        qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.CHO_DUYET_PHIEU_DE_NGHI;
                    else
                        qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.CHINH_SUA_PHIEU_DE_NGHI;
                    
                    dc.QuyTrinhMuaHangs.InsertOnSubmit(qt);
                }
                else
                {
                    phieu = dc.PhieuDeNghiMuaHangs.Single(p => p.MaPhieu == _maPhieu);
                    if (sender == xinDuyetBtn)
                        phieu.QuyTrinhMuaHangs.Single().TrangThai = _DB.TrangThaiQuyTrinhMuaHang.CHO_DUYET_PHIEU_DE_NGHI;
                }

                // update info
                phieu.NgayHan = dtpNgayHan.Value;
                phieu.NgayTao = dtpNgayTao.Value;
                phieu.NoiDung = txtContent.Text;

                // Tao hang hoa
                dc.PhieuDeNghiMuaHang_HangHoas.DeleteAllOnSubmit(phieu.PhieuDeNghiMuaHang_HangHoas);
                foreach (DataGridViewRow r in dgvHangHoa.Rows)
                    if (!r.IsNewRow)
                    {
                        _DB.PhieuDeNghiMuaHang_HangHoa hanghoa = new _DB.PhieuDeNghiMuaHang_HangHoa();
                        hanghoa.MaHangHoa = ((_DB.HangHoa)r.Cells[1].Value).MaHangHoa;
                        hanghoa.SoLuongDeNghi = float.Parse(r.Cells[2].Value as string);
                        hanghoa.NhaCungCapThamKhao = (string)r.Cells[4].Value;
                        if (r.Cells[5].Value != null && !r.Cells[5].Value.Equals(""))
                            hanghoa.GiaThamKhao = float.Parse(r.Cells[5].Value as string);

                        phieu.PhieuDeNghiMuaHang_HangHoas.Add(hanghoa);
                    }

                if (!_edit)
                    dc.PhieuDeNghiMuaHangs.InsertOnSubmit(phieu);

                // submit
                _maPhieu = phieu.MaPhieu;
                dc.SubmitChanges();
                _success = true;
                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public string getMaPhieu()
        {
            return _maPhieu;
        }

        public bool getSuccess()
        {
            return _success;
        }

        private void dgvHangHoa_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            dgvHangHoa.CommitEdit(DataGridViewDataErrorContexts.Commit);

            if (dgvHangHoa.SelectedCells.Count == 1)
            {
                DataGridViewCell c = dgvHangHoa.SelectedCells[0];
                if (c.ColumnIndex == 1 && c.RowIndex >= 0)
                {
                    _DB.HangHoa h = (_DB.HangHoa)dgvHangHoa.Rows[c.RowIndex].Cells[c.ColumnIndex].Value;
                    if (h != null)
                    {
                        dgvHangHoa.Rows[c.RowIndex].Cells[3].Value = h.DonVi;
                        dgvHangHoa.Rows[c.RowIndex].Cells[5].Value = h.Gia.ToString();
                    }

                }
            }
        }

    }
}
